/*
 * Meta language compiler
 * Copyright (C) 2014  Sergey Vidyuk <sir.vestnik@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <cassert>

#include "parser/function.h"
#include "parser/call.h"
#include "parser/vardecl.h"

namespace meta {

Call::Call(const StackFrame* reduction, size_t size):
    Node(reduction, size),
    mFunction(nullptr)
{
    assert(size == 4);
    mFunctionName = reduction[0].tokens;
}

void Call::setFunction(Function *func)
{
    mFunction = func;
    // use default args if needed and possible
    auto declaredArgs = func->args();
    for (size_t pos = children.size(); pos < declaredArgs.size(); ++pos) {
        auto defaultVal = declaredArgs[pos]->initExpr();
        if (!defaultVal)
            break;
        children.push_back(defaultVal);
    }
}

Node *Call::arg(size_t pos)
{
    if (pos < children.size())
        return children[pos];
    return nullptr;
}

size_t Call::argsCount() const
{
    return children.size();
}

}
